package com.bgy.test;

import com.jfinal.kit.PropKit;
import com.jfinal.qyweixin.sdk.api.ApiConfig;
import com.jfinal.qyweixin.sdk.api.ApiConfigKit;
import com.jfinal.qyweixin.sdk.api.ApiResult;
import com.jfinal.qyweixin.sdk.api.ConDepartmentApi;
import com.jfinal.qyweixin.sdk.api.ConUserApi;

public class UserSyn {
	
	public static void main(String[] args) {
		txl();
	}
	
	public static void init(){
		loadProp("a_little_config_pro.txt", "a_little_config.txt");
		//ApiConfigKit.setThreadLocalApiConfig(getApiConfig());
		ApiConfigKit.putApiConfig(getApiConfig());
	}
	
	//通讯录获取并实现同步 1.getDepartment()
	//{"errcode":0,"errmsg":"ok","department":[{"id":1,"name":"移动平台开发","parentid":0,"order":2147483447}]}
	//{"errcode":0,"errmsg":"ok","userid":"hecore","name":"何杰","department":[1],"position":"","mobile":"18235436145","gender":"1","email":"hecore@163.com","avatar":"http://shp.qpic.cn/bizmp/2OxFHy6eTCVSiawRC6dTVic1iavPpdQULRpusiaLFEwk7dAeAQiccwrCq9g/","status":1,"isleader":0,"extattr":{"attrs":[]},"english_name":"","telephone":"","enable":1,"hide_mobile":0,"order":[0]}
	public static void txl(){
		init();
		System.out.println(getDepartment());
		System.out.println(getUser());
	} 
	
	public static String getDepartment() {
		ApiResult apiResult = ConDepartmentApi.getDepartment("1");
		return apiResult.getJson();
	}
	
	public static String getUser() {
		//这里需要对hecore进行处理然后就可以获取到用户的信息
		ApiResult apiResult = ConUserApi.getUser("hecore");
		return apiResult.getJson();
	}
	
	/**
	 * 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
	 * @param pro 生产环境配置文件
	 * @param dev 开发环境配置文件
	 */
	private static void loadProp(String pro, String dev) {
		try {
			PropKit.use(pro);
		}
		catch (Exception e) {
			PropKit.use(dev);
		}
	}
	
	/**
	 * 如果要支持多公众账号，只需要在此返回各个公众号对应的  ApiConfig 对象即可
	 * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
	 */
	private static ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();		
		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setCorpId(PropKit.get("corpId"));
		ac.setCorpSecret(PropKit.get("secret"));
		ac.setAgentId(PropKit.get("agentId"));
		/**
		 *  是否对消息进行加密，对应于微信平台的消息加解密方式：
		 *  1：true进行加密且必须配置 encodingAesKey
		 *  2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey", "在配置文件中设置该属性"));
		return ac;
	}		
}
